<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="cn.estore.util.DBConnection" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>用户信息查询结果</title>
	<style>
		body {
			font-family: Arial, sans-serif;
			background-color: #f4f4f4;
			text-align: center;
		}

		h1 {
			color: #333;
			margin-top: 20px;
		}

		table {
			width: 80%;
			margin: 20px auto;
			border-collapse: collapse;
			box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
		}

		th, td {
			padding: 15px;
			text-align: left;
			border-bottom: 1px solid #ccc;
		}

		th {
			background-color: #555;
			color: white;
		}

		tr:nth-child(even) {
			background-color: #f9f9f9;
		}

		tr:hover {
			background-color: #eaeaea;
		}
	</style>
</head>
<body>
<%
	request.setCharacterEncoding("UTF-8");
	String username = request.getParameter("username");

	Connection conn = null;
	PreparedStatement stmt = null;
	ResultSet rs = null;
	try {
		// 获取数据库连接
		DBConnection jdbc = new DBConnection();
		conn = jdbc.getConnection();
		// 使用PreparedStatement防止SQL注入
		String sql = "select * from user where username =?";
		stmt = conn.prepareStatement(sql);
		stmt.setString(1, username);
		rs = stmt.executeQuery();
%>
<h1>用户信息查询结果</h1>
<table>
	<tr>
		<th>序号</th>
		<th>用户名</th>
		<th>真实姓名</th>
		<th>年龄</th>
		<th>性别</th>
		<th>密码</th>
		<th>专业</th>
		<th>爱好</th>
		<th>邮箱</th>
	</tr>
	<%
		// 循环遍历结果集，将数据填充到表格行中
		while (rs.next()) {
	%>
	<tr>
		<td><%=rs.getString(1)%></td>
		<td><%=rs.getString(2)%></td>
		<td><%=rs.getString(3)%></td>
		<td><%=rs.getInt(4)%></td>
		<td><%=rs.getString(5)%></td>
		<td><%=rs.getString(6)%></td>
		<td><%=rs.getString(7)%></td>
		<td><%=rs.getString(8)%></td>
		<td><%=rs.getString(9)%></td>
	</tr>
	<%
		}
	%>
</table>
<%
	} catch (SQLException e) {
		// 捕获数据库操作异常，输出提示信息告知用户
		out.println("数据库查询出现异常，请稍后再试！");
		e.printStackTrace();
	} finally {
		// 使用try-with-resources语句自动关闭资源，无需手动编写关闭代码，增强代码可靠性
		try {
			if (rs!= null) {
				rs.close();
			}
			if (stmt!= null) {
				stmt.close();
			}
			if (conn!= null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
%>
</body>
</html>
